#!/bin/sh

TOP_DIR=$(cd $(dirname $0)/../../ && pwd)
. $TOP_DIR/tools/docker/config $* >/dev/null 2>&1

[ ! -d $LAB_OUTPUT ] && exit 0

[ -z "$ENCRYPT_CMD" ] && ENCRYPT_CMD=md5sum
[ -z "$TOKEN_LENGTH" ] && TOKEN_LENGTH=$DEF_TOKEN_LENGTH

labs=`find $LAB_OUTPUT -maxdepth 1 -mindepth 1 | sort`

get_var HOST localhost

get_lab_attr () # lab_attr
{
  lab_user=$1
  lab_life=$2
  lab_start=$3$4
}

for lab in $labs
do
    lab_basename=$(basename $lab)
    lab_attr=$(echo $lab_basename | cut -d'+' -f2 | tr '-' ' ')

    get_lab_attr $lab_attr

    lab_name=$(echo $lab_basename | cut -d'+' -f1)

    TOP_DIR=$lab
    unset UNIX_USER UNIX_PWD VNC_PWD VNC_PWD_VIEWONLY CONTAINER_NAME CONTAINER_IP CONTAINER_TOKEN
    . $TOP_DIR/tools/docker/config $lab_name >/dev/null 2>&1

    CONTAINER_IP=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' $CONTAINER_NAME`
    CONTAINER_TOKEN=`echo -n $CONTAINER_IP | $ENCRYPT_CMD | cut -d' ' -f1 | cut -c1-$TOKEN_LENGTH`

    do_unlock
    get_var UNIX_USER ubuntu
    do_lock

    echo $lab_name $lab_user $HOST $HOST_VNC_PORT $CONTAINER_TOKEN $VNC_PWD $VNC_PWD_VIEWONLY $CONTAINER_IP $HOST_WEBSSH_PORT $UNIX_PWD $CONTAINER_NAME $lab_life $lab_start
done
